import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:library_base/base/base_app_state_page.dart';
import 'package:library_base/provider/base_list_controller.dart';

abstract class BaseListUi {
  EasyRefreshController controller = EasyRefreshController(
    controlFinishRefresh: true,
    controlFinishLoad: true,
  );

  Header? header;

  Footer? footer;

  ///头部文字颜色
  Color headerTextColor = Colors.black;

  ///脚部文字颜色
  Color footerTextColor = Colors.teal;

  ///脚部文字颜色
  Color headerInfoColor = Colors.teal;

  Color footerInfoColor = Colors.teal;

  ///是否能下拉刷新
  bool _isCanRefresh = true;

  ///是否能上拉加载
  bool isCanLoad = false;

  ScrollController? _scrollController;

  Axis scrollDirection = Axis.vertical;

  bool shrinkWrap = false;

  ///是否支持刷新
  bool useRefresh = false;

  BaseState? _baseState;

  ///可否手动控制加载和刷新
  bool enableControlFinishLoad = false;
  bool enableControlFinishRefresh = false;

  Color? appBarDefaultColor;
  Color? appBarContentDefaultColor;

  ///滑动偏移量监听， 用于手势处理
  Function(double)? _offsetListen;

  Function(double)? mOffsetListen;

  ///头部回弹
  bool topBouncing = false;

  ///底部回弹
  bool bottomBouncing = false;

  ///控制器
  BaseListController? baseController;

  listInit() {}

  ///挂载 基础state
  void registerStateForList(BaseState baseState) {
    header = _getDefaultHeader();
    footer = _getDefaultFooter();
    _baseState = baseState;
    appBarDefaultColor = baseState.getNavBarBackgroundColor();
    appBarContentDefaultColor = baseState.getNavBarContentColor();
    mOffsetListen = (offset) {
      ///处理头部缩放
      _offsetListen?.call(offset);
    };
    scrollController = ScrollController();
  }

  Widget getListView(BuildContext context, BaseListController? baseController) {
    this.baseController = baseController;
    List<Widget> listChildren = buildListItem(context);
    return EasyRefresh(
        onRefresh: isUseRefresh && isCanRefresh ? onRefreshCallback : null,
        onLoad: (isUseRefresh &&
                ((bottomBouncing) ||
                    (baseController?.enableLoadMore ?? false) ||
                    isCanLoad ||
                    (baseController?.isShowNoMoreFooter ?? false)))
            ? onLoadCallback
            : null,
        // header: isUseRefresh ? header : null,
        // footer: isUseRefresh ? footer : null,
        controller: isUseRefresh ? controller : null,
        noMoreRefresh: isUseRefresh ? enableControlFinishRefresh : false,
        noMoreLoad: isUseRefresh ? enableControlFinishLoad : false,
        scrollController: scrollController,
        child: getAdapterWidget(listChildren));
  }

  ///返回不为空 则 依据 getMainChildrenWidget 来 绘制 ，
  ///否则根据 [getItemCount] 和  [getItem]来返回 列表
  List<Widget> buildListItem(BuildContext context) => [];

  ///默认是ListView,若子组件不是ListView则重写
  Widget getAdapterWidget(List<Widget> listChildren) {
    if (baseController == null) {
      return Container();
    }
    return Material(
      color: Colors.transparent,
      child: MediaQuery.removePadding(
        context: baseController!.context!,
        removeTop: true,
        removeBottom: true,
        child: ListView(
          children: listChildren,
        ),
      ),
    );
  }

  Future onRefreshCallback() async {
    await baseController?.onRefreshCallback();
  }

  Future onLoadCallback() async {
    if (baseController?.enableLoadMore == true) {
      await baseController?.onLoadCallback();
    }
  }

  Footer? _getDefaultFooter() => null;

  Header? _getDefaultHeader() => null;

  bool get isCanRefresh => _isCanRefresh || (baseController?.isCanRefresh ?? false);

  set isCanRefresh(bool value) {
    _isCanRefresh = value;
    baseController?.isCanRefresh = value;
  }

  ScrollController get scrollController => _scrollController!;

  set scrollController(ScrollController scrollControl) {
    _scrollController = scrollControl;
    _addOffsetListen();
  }

  set offsetListen(Function(double) offsetListen) {
    _offsetListen = offsetListen;
  }

  bool get isUseRefresh => useRefresh || (baseController?.useRefresh ?? false);

  set isUseRefresh(bool value) {
    useRefresh = value;
    baseController?.useRefresh = value;
  }

  void setSafeState() {
    _baseState?.setSafeState();
  }

  void _addOffsetListen() {
    scrollController.addListener(() {
      mOffsetListen?.call(scrollController.offset);
    });
  }

  // _getFooter() {
  //   if (baseController?.enableLoadMore ?? true) {
  //     return footer;
  //   } else if ((baseController?.isShowNoMoreFooter ?? false) &&
  //       (!(baseController?.isEmpty ?? true))) {
  //     return getNoMoreFooter();
  //   }
  //   return EmptyFooter(overScroll: bottomBouncing);
  // }

  // Footer getNoMoreFooter() {
  //   return NoMoreFooter(overScroll: bottomBouncing);
  // }
}
